import {
  HTTP
} from '../utils/http-p.js';

class ClassicModel extends HTTP {
  getLatest() {
    return this.request({
      url: 'classic/latest'
    });
    // this.request({
    //   url: 'classic/latest',
    //   success: (res) => {
    //     scallback(res);
    //     // 写入缓存中
    //     let key = this._getKey(res.index);
    //     wx.setStorageSync(key, res);
    //     this._setLatestIndex(res.index);
    //   }
    // })
  }

  getClassic(index, nextOrPrevious) {
    // if (!classic) {
      return this.request({
        url: `classic/${index}/${nextOrPrevious}`
      });
    // }
  }
  // getClassic(index, nextOrPrevious, sCallback) {
  //   let key = nextOrPrevious == 'next' ? this._getKey(index + 1) : this._getKey(index - 1);
  //   // 从缓存中获取
  //   let classic = wx.getStorageSync(key);
  //   if (!classic) {
  //     this.request({
  //       url: `classic/${index}/${nextOrPrevious}`,
  //       success: (res) => {
  //         // 存入缓存中
  //         wx.setStorageSync(this._getKey(res.index), res);
  //       }
  //     });
  //   } else {
  //     sCallback(classic);
  //   }
  // }

  getMyFavor() {
    return this.request({
      url: "classic/favor"
    });
  }

  isFirst(index) {
    return index == 1 ? true : false;
  }

  isLatest(index) {
    let latestIndex = this._getLatestIndex();
    return latestIndex == index ? true : false;
  }

  _setLatestIndex(index) {
    wx.setStorageSync('latest', index);
  }

  _getLatestIndex() {
    let index = wx.getStorageSync('latest');
    return index;
  }

  _getKey(index) {
    let key = `classic-${index}`;
    return key;
  }

}




export {
  ClassicModel
}